package com.next

import groovy.transform.CompileStatic
import groovy.transform.TypeChecked

import java.text.DecimalFormat

@CompileStatic
@TypeChecked
class BillsService
{
    def componentService
    /**
     * 重置计划
     * */
    def reset(Opportunity opportunity)
    {
        if (opportunity?.type.name != "抵押贷款")
        {
            opportunity = opportunity?.parent
        }
        //订单期数
        Calendar calendar = Calendar.getInstance()
        calendar.clear()
        calendar.setTime(opportunity?.actualLendingDate)
        int day = calendar.get(Calendar.DAY_OF_MONTH)
        //上扣息月份数
        def firstPayOfMonthes = opportunity?.monthOfAdvancePaymentOfInterest?.intValue() == 0 ? 1 : opportunity?.monthOfAdvancePaymentOfInterest?.intValue()
        if (day >= 15 && day < 20)
        {
            if (opportunity?.interestPaymentMethod?.name == "扣息差")
            {
                firstPayOfMonthes = 2
            }
        }
        if (day == 20)
        {
            firstPayOfMonthes += 1
        }
        def bills = Bills.find("from Bills where opportunity.id = ${opportunity.id}")
        def billsItems = BillsItem.findAll("from BillsItem where bill = ? and overdue = ? ",[bills, true])
        billsItems.each { it->
            it.overdue = false
            it.status = "待收"
            it.transactionRecord.status = TransactionRecordStatus.find("from TransactionRecordStatus where name = '未执行'")
            it.transactionRecord.save()
            it.save()
        }
        billsItems = BillsItem.findAll("from BillsItem where bills = ? and prepayment = ? ",[bills, true])
        billsItems.each {
            it.prepayment = false
            it.status = "待收"
            it.transactionRecord.status = TransactionRecordStatus.find("from TransactionRecordStatus where name = '未执行'")
            it.transactionRecord.save()
            it.save()
        }
        billsItems = BillsItem.findAll("from BillsItem where bills = ? and split = ? ",[bills, true])
        billsItems.each {it ->
            it.split = false
            it.status = "待收"
            it.transactionRecord.status = TransactionRecordStatus.find("from TransactionRecordStatus where name = '未执行'")
            it.transactionRecord.save()
            it.save()
        }
        billsItems = BillsItem.findAll("from BillsItem where bills = ? and period = ? and type = ? ",[bills, opportunity?.actualLoanDuration - firstPayOfMonthes + 1, BillsItemType.find("from BillsItemType where name = '利息违约金'")])
        billsItems.each { it->
            def transactionRecord = TransactionRecord.find("from TransactionRecord where id = ${it.transactionRecordId}")
            transactionRecord.amount -= it.receivable
            transactionRecord.save()

        }
        billsItems.each {

            it.delete()

        }
        billsItems = BillsItem.findAll("from BillsItem where bills = ? and period= ?",[bills, opportunity?.actualLoanDuration - firstPayOfMonthes + 2])
        billsItems.each {com.next.BillsItem it->
            it.transactionRecord.status = TransactionRecordStatus.find("from TransactionRecordStatus where name = '已失效'")
            it.save()
        }
        billsItems.each {com.next.BillsItem
            it.delete()

        }
    }

    /**
     * 是否已经处理过早偿或者逾期*/
    def alreadyDealPrePayOrOverdue(Opportunity opportunity)
    {
        def bills = Bills.find("from Bills where opportunity.id = ${opportunity}")
        def billsItems = BillsItem.findAll("from BillsItem where bills = ? and status = ?",[bills, "待收"])
        if (billsItems.size() > 0)
        {
            for (
                def i = 0;
                i < billsItems.size();
                i++)
            {
                if (billsItems[i].type?.name == "利息违约金" || billsItems[i].type?.name == "早偿违约金" || billsItems[i].type?.name == "本金违约金")
                {
                    return true
                }
            }
        }
        return false
    }

    /**
     * 数字转换为汉语中人民币的大写<br>
     *
     * @author nagelan
     * @create 2017-07-17
     */
    def transforNumToRMB(Double value)
    {
        DecimalFormat format = new DecimalFormat("#.00")
        def str = format.format(value)
        def s = str.split("\\.")
        String  temp = ""
        int ling = 0
        int shu = 0
        int pos = 0
        def s1 = s[0].toCharArray()
        def s2 = s[1].toCharArray()
        for (
            int j = 0;
            j < s[0].length();
            ++j)
        {
            int num =  (int)s1[j] - 48
            if (num == 0)
            {
                ling++
                if (ling == s[0].length())
                {
                    temp = "零"
                } else if (s[0].length() - j - 1 == 4)
                {
                    if (shu == 1 && (s[0].length() - pos - 1) >= 5 && (s[0].length() - pos - 1) <= 7)
                    {
                        temp += "万"
                    }
                } else if (s[0].length() - j - 1 == 8)
                {
                    if (shu == 1 && (s[0].length() - pos - 1) >= 9 && (s[0].length() - pos - 1) <= 11)
                    {
                        temp += "亿"
                    }
                }
            } else
            {
                shu++
                int flag = 0
                if (shu == 1)
                {
                    ling = 0
                    pos = j
                }
                if (shu == 2)
                {
                    flag = 1
                    if (ling > 0)
                    {
                        temp += "零"
                    }
                    shu = 1
                    pos = j
                    ling = 0
                }
                if (s[0].length() - j - 1 == 11)
                {
                    temp += ToBig(num) + "仟"
                } else if (s[0].length() - j - 1 == 10)
                {
                    temp += ToBig(num) + "佰"
                } else if (s[0].length() - j - 1 == 9)
                {
                    if (num == 1 && flag != 1)
                    {
                        temp += "拾"
                    } else
                    {
                        temp += ToBig(num) + "拾"
                    }
                } else if (s[0].length() - j - 1 == 8)
                {
                    temp += ToBig(num) + "亿"
                } else if (s[0].length() - j - 1 == 7)
                {
                    temp += ToBig(num) + "仟"
                } else if (s[0].length() - j - 1 == 6)
                {
                    temp += ToBig(num) + "佰"
                } else if (s[0].length() - j - 1 == 5)
                {
                    if (num == 1 && flag != 1)
                    {
                        temp += "拾"
                    } else
                    {
                        temp += ToBig(num) + "拾"
                    }
                } else if (s[0].length() - j - 1 == 4)
                {
                    temp += ToBig(num) + "万"
                } else if (s[0].length() - j - 1 == 3)
                {
                    temp += ToBig(num) + "仟"
                } else if (s[0].length() - j - 1 == 2)
                {
                    temp += ToBig(num) + "佰"
                } else if (s[0].length() - j - 1 == 1)
                {
                    if (num == 1 && flag != 1)
                    {
                        temp += "拾"
                    } else
                    {
                        temp += ToBig(num) + "拾"
                    }
                } else
                {
                    temp += ToBig(num)
                }
            }
        }
        temp += "元"
        for (
            int j = 0;
            j < s[1].length();
            ++j)
        {
            int num = (int)s2[j] - 48
            if (j == 0)
            {
                if (num != 0)
                {
                    temp += ToBig(num) + "角"
                } else if (num == 0 && 1 < s[1].length() && s2[1] != '0')
                {
                    temp += "零"
                }
            } else if (j == 1)
            {
                if (num != 0)
                {
                    temp += ToBig(num) + "分"
                }
            }
        }
        println(temp)
        return temp
    }

    /**
     * @Author Nagelan
     * @param num
     * @return 阿拉伯数字转中文数字正写
     * @date 2017/7/18 10:39
     */
    String ToBig(int num)
    {
        def str = ["壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾"]
        return str[num - 1]
    }

}
